/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.yunhoo.sys.util.common;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Administrator
 */
public class EmailTool {
    //审核时发送
    public static void sendMail(String sendAddress,String receiveAddress,String pwd,String title,String content){
        try{
            Session session = emaiSession();
            Message msg = setMsg(session,sendAddress,receiveAddress,title,content); //建立信息
            Transport tran = session.getTransport("smtp");
            tran.connect("smtp.163.com",sendAddress,pwd);
            tran.sendMessage(msg, msg.getAllRecipients()); // 发送
            System.out.println("邮件发送成功");
        }
        catch(Exception e){
            System.out.println(e.getCause());
        }
    }
    //设置邮件信息
    public static Message setMsg(Session session,String sendAddress,String receiveAddress,String title,String content){
        Message msg = new MimeMessage(session);
        try{
            msg.setFrom(new InternetAddress(sendAddress));//发件人
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveAddress)); //收件人
            msg.setSentDate(new Date()); // 发送日期
            msg.setSubject(title); // 主题
            msg.setText(content); //内容
        }
        catch(Exception e){
            System.out.println(e.getCause());
        }
        return msg;
    }
    //邮件服务器信息
    public static Session emaiSession(){
        Properties p = new Properties();
        p.put("mail.smtp.auth", "true");
        p.put("mail.transport.protocol", "smtp");
        p.put("mail.smtp.host", "smtp.163.com");
        p.put("mail.smtp.port", "25");
        return Session.getInstance(p);
    }
}
